package org.edu.modules.center.controller;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.edu.common.api.vo.Result;
import org.edu.common.aspect.annotation.AutoLog;
import org.edu.common.system.base.controller.BaseController;
import org.edu.common.system.query.QueryGenerator;
import org.edu.common.system.vo.LoginUser;
import org.edu.modules.center.entity.VeStudyProgress;
import org.edu.modules.center.service.IVeStudyProgressService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

@Api(tags={"学生学习进度记录"})
@RestController
@RequestMapping({"/center/veStudyProgress"})
public class VeStudyProgressController
        extends BaseController<VeStudyProgress, IVeStudyProgressService>
{
    private static final Logger log = LoggerFactory.getLogger(VeStudyProgressController.class);
    @Autowired
    private IVeStudyProgressService veStudyProgressService;

    @AutoLog("学生学习进度记录-分页查询")
    @ApiOperation(value="分页查询", notes="学生学习进度记录-分页查询")
    @PostMapping({"/list"})
    public Result<?> queryPageList(VeStudyProgress veStudyProgress, @RequestParam(name="pageNo", defaultValue="1") Integer pageNo, @RequestParam(name="pageSize", defaultValue="10") Integer pageSize, HttpServletRequest req)
    {
        QueryWrapper<VeStudyProgress> queryWrapper = QueryGenerator.initQueryWrapper(veStudyProgress, req.getParameterMap());
        Page<VeStudyProgress> page = new Page(pageNo.intValue(), pageSize.intValue());
        IPage<VeStudyProgress> pageList = this.veStudyProgressService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    @AutoLog("学生学习进度记录-通过id查询")
    @ApiOperation(value="通过id查询", notes="学生学习进度记录-通过id查询")
    @PostMapping({"/queryById"})
    public Result<?> queryById(@RequestParam(name="id", required=true) String id)
    {
        VeStudyProgress veStudyProgress = (VeStudyProgress)this.veStudyProgressService.getById(id);
        if (veStudyProgress == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(veStudyProgress);
    }

    @AutoLog("学生学习进度记录-新增信息")
    @ApiOperation(value="新增信息", notes="学生学习进度记录-新增信息")
    @PostMapping({"/add"})
    public Result<?> add(@RequestBody VeStudyProgress veStudyProgress)
    {
        this.veStudyProgressService.save(veStudyProgress);
        return Result.OK("添加成功！");
    }

    @AutoLog("学生学习进度记录-修改信息")
    @ApiOperation(value="修改信息", notes="学生学习进度记录-修改信息")
    @PostMapping({"/edit"})
    public Result<?> edit(@RequestBody VeStudyProgress veStudyProgress)
    {
        this.veStudyProgressService.updateById(veStudyProgress);
        return Result.OK("编辑成功!");
    }

    @AutoLog("学生学习进度记录-通过id删除")
    @ApiOperation(value="通过id删除", notes="学生学习进度记录-通过id删除")
    @PostMapping({"/delete"})
    public Result<?> delete(@RequestParam(name="id", required=true) String id)
    {
        this.veStudyProgressService.removeById(id);
        return Result.OK("删除成功!");
    }

    @AutoLog("学生学习进度记录-批量删除")
    @ApiOperation(value="批量删除", notes="学生学习进度记录-批量删除")
    @PostMapping({"/deleteBatch"})
    public Result<?> deleteBatch(@RequestParam(name="ids", required=true) String ids)
    {
        this.veStudyProgressService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    @ApiOperation(value="显示列表", notes="学生学习进度记录-显示列表")
    @PostMapping({"/pageList"})
    public Result<?> pageList()
    {
        List<VeStudyProgress> teachAsks = this.veStudyProgressService.nodeList();
        return Result.OK(teachAsks);
    }

    @AutoLog("学生学习进度记录-查询栏目树")
    @ApiOperation(value="章节统计", notes="学生学习进度记录-查询栏目树")
    @PostMapping({"/queryTree"})
    @ApiImplicitParams({@io.swagger.annotations.ApiImplicitParam(name="tcid", value="教学课程id", required=false), @io.swagger.annotations.ApiImplicitParam(name="studentid", value="学生id", required=false)})
    public Result<?> queryTree(@ApiIgnore VeStudyProgress veStudyProgress, HttpServletRequest req)
    {
        if (veStudyProgress.getStudentid().equals(Integer.valueOf(0)))
        {
            LoginUser sysUser = (LoginUser)SecurityUtils.getSubject().getPrincipal();
            veStudyProgress.setStudentid(Integer.valueOf(sysUser.getUserId()));
        }
        QueryWrapper<VeStudyProgress> queryWrapper = QueryGenerator.initQueryWrapper(veStudyProgress, req.getParameterMap());
        queryWrapper.setEntity(veStudyProgress);
        JSONArray jArray = this.veStudyProgressService.queryTree(queryWrapper);
        return Result.OK(jArray);
    }

    @AutoLog("学生学习进度记录-未看完视频的章节数量")
    @ApiOperation(value="未看完视频的章节数量", notes="学生学习进度记录-未看完视频的章节数量")
    @PostMapping({"/queryCourseId"})
    public Result<?> queryCourseId(@RequestParam(name="courseid", required=true) String courseid, @RequestParam(name="studentid", required=true) String studentid, HttpServletRequest req)
    {
        int count = this.veStudyProgressService.selectCount(courseid, studentid);
        return Result.OK(Integer.valueOf(count));
    }
}
